﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcDeviceManager.Models;
using System.Collections;
using MvcDeviceManager.Util;

namespace MvcDeviceManager.Service
{
    public class RoleAuthorityService
    {

        public bool hasAuthority(string[] roles, string ActionName, string ControllerName)
        {
            IList<RoleAuthority> list = new List<RoleAuthority>();
            RoleAuthority temp = new RoleAuthority();
            temp.ActionId = ActionName;
            temp.Controller = ControllerName;
            foreach (string role in roles)
            {
                temp.Role = role;
                IList<RoleAuthority> listT = DaoUtil.getSqlMapper().QueryForList<RoleAuthority>("MvcDeviceManager.Models.RoleAuthority.selectRoleAuthorityByRoles", temp);
                foreach (RoleAuthority ra in listT)
                {
                    list.Add(ra);
                }
            }
            return list.Count > 0;           
        }

        public IList<RoleAuthority> listRoleAuthority(string role)
        {

            IList<RoleAuthority> list = DaoUtil.getSqlMapper().QueryForList<RoleAuthority>("MvcDeviceManager.Models.RoleAuthority.selectAuthorityByRole", role);
            return list;
        }

        public void updateRoleAuthority(string role, IList<RoleAuthority> list)
        {
            DaoUtil.getSqlMapper().Delete("MvcDeviceManager.Models.RoleAuthority.deleteByRole", role);
            foreach(RoleAuthority ra in list){
                DaoUtil.getSqlMapper().Insert("MvcDeviceManager.Models.RoleAuthority.insert", ra);
            }
        }

        public IList<RoleAuthority> listUsersRoleAllAuthority(string[] roles)
        {
            IList<RoleAuthority> list = DaoUtil.getSqlMapper().QueryForList<RoleAuthority>("MvcDeviceManager.Models.RoleAuthority.selectRoleAuthorityByRoleList", roles);
            foreach (string role in roles)
            {

                IList<RoleAuthority> listT = listRoleAuthority(role);
                foreach (RoleAuthority ra in listT)
                {
                    list.Add(ra);
                }
            }
            return list;
        }
    }
}